var showTrackValue = (field, oldVal) => {
  if (window.location.pathname === '/CarUnderwriting/proposal' || window.location.pathname === '/CarUnderwriting/verification/detail' || window.location.pathname === '/CarUnderwriting/newCheck') {
    if (!field.trackLabel) {
      field.trackLabel = document.createElement("div");
      field.trackLabel.className = "el-form-item__track el-form-item__error";
      if (field.validateState === "error") {
        field.trackLabel.style = "display:none";
      }
      field.$el.querySelector(".el-form-item__content").appendChild(field.trackLabel);
      field.$el.classList.add("is-track");
    }
    if (!oldVal) {
      oldVal = "";
    } else {
      field.trackLabel.innerHTML = `原值：${oldVal}`;
    }
  }

};

var compareFunc = (form, field) => {
  form.$emit("form-item-change");

  // 这个处理不需要比值的字段 又必须写prop的字段
  if (field.$attrs.hasOwnProperty("formTrackPass")) {
    return;
  }
  let oldVal = form.trackValue;
  if (oldVal) {
    let newVal = form.model;
    if(field.prop&&field.prop.split(".").indexOf('engage')>-1&&field.prop.split(".").length>3){
      let newEngage = {}
      let oldEngage = {}
      if(field.prop.split('.').indexOf('biContract') > -1){
        field.prop && field.prop.split(".").forEach(p => {
          if (newVal && p === 'engage') {
            newEngage = newVal.biContract[p]
          }
        });
        field.prop && field.prop.split(".").forEach(p => {
          if (oldVal && p === 'engage') {
            oldEngage = oldVal.biContract[p];
          }
        });
      }else{
        field.prop && field.prop.split(".").forEach(p => {
          if (newVal && p === 'engage') {
            newEngage = newVal.ciContract[p]
          }
        });
        field.prop && field.prop.split(".").forEach(p => {
          if (oldVal && p === 'engage') {
            oldEngage = oldVal.ciContract[p];
          }
        });
      }
      newVal = newEngage[field.prop.split(".")[2]].engageDetail
      let oldIndex = null
      oldEngage.map((item,index)=>{
        if(newEngage[field.prop.split(".")[2]].engageCode === item.engageCode){
          oldIndex = index
        }
      })
      if(oldIndex == null){
        oldVal = newVal
      }else{
        oldVal = oldEngage[oldIndex].engageDetail
      }
    }else{
      field.prop && field.prop.split(".").forEach(p => {
        if (newVal) {
          newVal = newVal[p];
        }
      });
      field.prop && field.prop.split(".").forEach(p => {
        if (oldVal) {
          oldVal = oldVal[p];
        }
      });
    }
    let valueType = typeof oldVal;
    if (newVal && typeof newVal === "object") {
      newVal = JSON.stringify(newVal);
    }
    if (oldVal && typeof oldVal === "object") {
      oldVal = JSON.stringify(oldVal);
    }
    if (field && field.$slots && field.$slots.default && field.$slots.default.length > 0 && field.$slots.default[0] && field.$slots.default[0].componentInstance && field.$slots.default[0].componentInstance.$options._componentTag === "el-date-picker") {
      let dateFormat = field.$slots.default[0].componentInstance.$options.propsData.valueFormat;
      if (dateFormat && oldVal) {
        oldVal = new Date(oldVal.replace(/-/g, "/")).format(
          dateFormat.replace("HH", "hh")
        );
      }
      if (dateFormat && newVal) {
        newVal = new Date(newVal.replace(/-/g, "/")).format(
          dateFormat.replace("HH", "hh")
        );
      }
    }
    if (newVal != oldVal && !(!newVal && !oldVal)) {
      if (field && field.$slots && field.$slots.default && field.$slots.default.length > 0 && field.$slots.default[0] && field.$slots.default[0].componentInstance && field.$slots.default[0].componentInstance.getTrackValue) {
        field.$slots.default[0].componentInstance.getTrackValue(
          valueType == "object" ? JSON.parse(oldVal) : oldVal,
          trcVal => {
            showTrackValue(field, trcVal.oldValue);
            form.$emit("form-track", {
              field: field,
              prop: field.prop,
              label: field.label,
              oldValue: trcVal.oldValue,
              newValue: typeof (trcVal.oldValue) === 'number' && (trcVal.newValue || trcVal.newValue === 0) ? +(trcVal.newValue) : trcVal.newValue
            });
          }
        );
      } else {
        if (field && field.$slots && field.$slots.default && field.$slots.default.length > 0 && field.$slots.default[0] && field.$slots.default[0].componentInstance && field.$slots.default[0].componentInstance.$options._componentTag === "el-select") {
          oldVal = ((field.$slots.default[0].componentInstance.getOption(oldVal).label && (field.$slots.default[0].componentInstance.getOption(oldVal).label).indexOf('-') !== -1) ? (field.$slots.default[0].componentInstance.getOption(oldVal).label).split('-')[1] : field.$slots.default[0].componentInstance.getOption(oldVal).label) || oldVal;
          newVal = ((field.$slots.default[0].componentInstance.getOption(newVal).label && (field.$slots.default[0].componentInstance.getOption(newVal).label).indexOf('-') !== -1) ? (field.$slots.default[0].componentInstance.getOption(newVal).label).split('-')[1] : field.$slots.default[0].componentInstance.getOption(newVal).label) || newVal;
        }
        if (field && field.$slots && field.$slots.default && field.$slots.default.length > 0 && field.$slots.default[0] && field.$slots.default[0].componentInstance && field.$slots.default[0].componentInstance.$options._componentTag === "el-radio-group") {
          let radioGroup = field.$slots.default[0].componentInstance;
          radioGroup.$children.forEach(radio => {
            if (
              radio.$options._componentTag === "el-radio" ||
              radio.$options._componentTag === "el-radio-button"
            ) {
              if (radio.label == oldVal) {
                oldVal = radio.$el.innerText;
              }
              if (radio.label == newVal) {
                newVal = radio.$el.innerText;
              }
            }
          });
        }
        showTrackValue(field, oldVal);
        form.$emit("form-track", {
          field: field,
          prop: field.prop,
          label: field.label,
          oldValue: oldVal,
          newValue: typeof oldVal === 'number' && (newVal || newVal === 0) ? +newVal : newVal
        });
      }
    } else {
      if (field.trackLabel) {
        field.$el
          .querySelector(".el-form-item__content")
          .removeChild(field.trackLabel);
        field.$el.classList.remove("is-track");
        field.trackLabel = null;
      }
      form.$emit("form-track", {
        field: field,
        prop: field.prop,
        label: field.label,
        oldValue: oldVal,
        newValue: typeof oldVal === 'number' && (newVal || newVal === 0) ? +newVal : newVal
      });
    }
  } else if (field.trackLabel) {
    field.$el
      .querySelector(".el-form-item__content")
      .removeChild(field.trackLabel);
    field.$el.classList.remove("is-track");
    field.trackLabel = null;
  }
};
export default {
  bind(el, binding, vnode) {
    vnode.componentInstance.trackValue = binding.value;
    vnode.componentInstance.$on("el.form.addField", field => {
      field.trackLabel = null;
      field.compareValue = () => compareFunc(vnode.componentInstance, field);
      if (vnode.componentInstance.trackValue) {
        field.compareValue();
        field.$on("el.form.change", field.compareValue);
      }
    });
    vnode.componentInstance.$on("el.form.removeField", field => {
      field.$off("el.form.change", field.compareValue);
      field.compareValue = () => compareFunc(vnode.componentInstance, field);
      field.compareValue();
    });

    vnode.componentInstance.trackValidate = (prop, valid) => {
      let field = vnode.componentInstance.fields.find(f => {
        return f.prop == prop;
      });

      vnode.componentInstance.$nextTick(() => {
        if (field && field.trackLabel) {
          if (valid) {
            field.trackLabel.style = "";
            field.$el.classList.add("is-track");
          } else {
            field.trackLabel.style = "display:none";
            field.$el.classList.remove("is-track");
          }
        }
      });
    };

    if (binding.value) {
      vnode.componentInstance.$on(
        "validate",
        vnode.componentInstance.trackValidate
      );
    }
  },
  update: function (el, binding, vnode) {
    if (vnode.componentInstance.trackValue != binding.value) {
      let oriValue = vnode.componentInstance.trackValue;
      vnode.componentInstance.trackValue = binding.value;
      if (binding.value) {
        vnode.componentInstance.fields.forEach(field => {
          field.compareValue();
          if (!oriValue) {
            field.$on("el.form.change", field.compareValue);
          }
        });
        if (!oriValue) {
          vnode.componentInstance.$on(
            "validate",
            vnode.componentInstance.trackValidate
          );
        }
      } else {
        vnode.componentInstance.fields.forEach(field => {
          field.compareValue(null);
          field.$off("el.form.change", field.compareValue);
        });
        vnode.componentInstance.$off(
          "validate",
          vnode.componentInstance.trackValidate
        );
      }
    }
  }
};
